﻿#include"XQHttpAccount.h"
#include"XSymbolOverload.h"
#include"XQAlgorithm.h"
using namespace HttpAccount;
Error HttpAccount::toError(const QString& str)
{
	if (str == "NoError")
		return Error::NoError;
	if (str == "NotLogin")
		return Error::NotLogin;
	if (str == "ServerError")
		return Error::ServerError;
	else if (str == "VerifyTimeout")
		return Error::VerifyTimeout;
	else if (str == "VerifyTimeNotArrived")
		return Error::VerifyTimeNotArrived;
	else if (str == "VerifyError")
		return Error::VerifyError;
	else if (str == "SendVerifyError")
		return Error::SendVerifyError;
	else if (str == "AccountNotExist")
		return Error::AccountNotExist;
	else if (str == "AccountDuplicated")
		return Error::AccountDuplicated;
	else if (str == "NewAccountFormatError")
		return Error::NewAccountFormatError;
	else if (str == "PasswordError")
		return Error::PasswordError;
	else if (str == "NewPasswordFormatError")
		return Error::NewPasswordFormatError;
	else if (str == "NewPasswordSameOld")
		return Error::NewPasswordSameOld;
	else if (str == "EmailError")
		return Error::EmailError;
	else if (str == "EmailBindOverboundCountError")
		return Error::EmailBindOverboundCountError;
	return Error::UnknownError;
}

QByteArray HttpAccount::error_toByteArray(Error e)
{
	switch (e)
	{
	case NoError:return "NoError";
		break;
	case NotLogin:return "NotLogin";
		break;
	case ServerError:return "ServerError";
		break;
	case VerifyTimeout:return "VerifyTimeout";
		break;
	case VerifyTimeNotArrived:return "VerifyTimeNotArrived";
		break;
	case VerifyError:return "VerifyError";
		break;
	case SendVerifyError:return "SendVerifyError";
		break;
	case AccountNotExist:return "AccountNotExist";
		break;
	case AccountDuplicated:return "AccountDuplicated";
		break;
	case NewAccountFormatError:return "NewAccountFormatError";
		break;
	case PasswordError:return "PasswordError";
		break;
	case NewPasswordFormatError:return "NewPasswordFormatError";
		break;
	case NewPasswordSameOld:return "NewPasswordSameOld";
		break;
	case EmailError:return "EmailError";
		break;
	case EmailBindOverboundCountError:return "EmailBindOverboundCountError";
		break;
	default:
		return "UnknownError";
		break;
	}
	return QByteArray();
}

QString HttpAccount::error_toChinese(Error e)
{
	switch (e)
	{
	case HttpAccount::NoError:return"无错误";
		break;
	case HttpAccount::NotLogin:return"未登录";
		break;
	case HttpAccount::ServerError:return"服务器错误";
		break;
	case HttpAccount::VerifyTimeout:return"验证码错误";
		break;
	case HttpAccount::VerifyTimeNotArrived:return"验证码更新时间未到";
		break;
	case HttpAccount::VerifyError:return"验证码错误";
		break;
	case HttpAccount::SendVerifyError:return"发送验证码失败";
		break;
	case HttpAccount::UnknownError:return"位置错误";
		break;
	case HttpAccount::AccountNotExist:return"账号不存在";
		break;
	case HttpAccount::AccountDuplicated:return"账号重复";
		break;
	case HttpAccount::NewAccountFormatError:return"新账号格式错误";
		break;
	case HttpAccount::PasswordError:return"密码错误";
		break;
	case HttpAccount::NewPasswordFormatError:return"新密码格式错误";
		break;
	case HttpAccount::NewPasswordSameOld:return"新密码与旧密码相同";
		break;
	case HttpAccount::EmailError:return"邮箱错误";
		break;
	case HttpAccount::EmailBindOverboundCountError:return"邮箱绑定账号数到达上限";
		break;
	default:
		break;
	}
	return QString();
}

Result HttpAccount::toResult(const QString& str)
{
	if (str == "AccountLogin")
		return Result::AccountLogin;
	if (str == "AccountLogout")
		return Result::AccountLogout;
	if (str == "AccountRegister")
		return Result::AccountRegister;
	if (str == "AccountInfo")
		return Result::AccountInfo;
	if (str == "AccountPortrait")
		return Result::AccountPortrait;
	if (str == "AccountNewEmailVerify")
		return Result::AccountNewEmailVerify;
	if (str == "AccountBindEmailVerify")
		return Result::AccountBindEmailVerify;
	if (str == "AccountModifyPassword")
		return Result::AccountModifyPassword;
	if (str == "AccountModifyInfo")
		return Result::AccountModifyInfo;
	if (str == "AccountModifyEmail")
		return Result::AccountModifyEmail;
	return Result::null;
}

QByteArray HttpAccount::result_toByteArray(Result result)
{
	switch (result)
	{
	case AccountLogin:return "AccountLogin";
		break;
	case AccountLogout:return "AccountLogout";
		break;
	case AccountRegister:return "AccountRegister";
		break;
	case AccountInfo:return "AccountInfo";
		break;
	case AccountPortrait:return "AccountPortrait";
		break;
	case AccountNewEmailVerify:return "AccountNewEmailVerify";
		break;
	case AccountBindEmailVerify:return "AccountBindEmailVerify";
		break;
	case AccountModifyPassword:return "AccountModifyPassword";
		break;
	case AccountModifyInfo:return "AccountModifyInfo";
		break;
	case AccountModifyEmail:return "AccountModifyEmail";
		break;
	default:
		return QByteArray();
		break;
	}
	return QByteArray();
}
QString HttpAccount::result_toChinese(Result result)
{
	switch (result)
	{
	case AccountLogin:return "账号登录";
		break;
	case AccountLogout:return "账号登出";
		break;
	case AccountRegister:return "账号注册";
		break;
	case AccountInfo:return "账号信息";
		break;
	case AccountPortrait:return "账号头像";
		break;
	case AccountNewEmailVerify:return "账号新邮箱验证码";
		break;
	case AccountBindEmailVerify:return "账号绑定邮箱验证码";
		break;
	case AccountModifyPassword:return "账号修改密码";
		break;
	case AccountModifyInfo:return "账号修改信息";
		break;
	case AccountModifyEmail:return "账号修改邮箱";
		break;
	default:
		return QString();
		break;
	}
	return QString();
}
int Verify::m_flushInterval = 1_mins;
int Verify::m_validInterval = 5_mins;
bool HttpAccount::Verify::isFlush() 
{
	QReadLocker lock(&m_lock);
	if (m_dateTime.isNull())
		return true;
	if (flushDateTime() < QDateTime::currentDateTime())
		return true;
	return false;
}

bool HttpAccount::Verify::isValid() 
{
	QReadLocker lock(&m_lock);
	if (m_dateTime.isNull())
		return true;
	if (validDateTime() > QDateTime::currentDateTime())
		return true;
	return false;
}

QDateTime HttpAccount::Verify::flushDateTime() 
{
	QReadLocker lock(&m_lock);
	return m_dateTime.addMSecs(m_flushInterval);
}

QDateTime HttpAccount::Verify::validDateTime() 
{
	QReadLocker lock(&m_lock);
	return m_dateTime.addMSecs(m_validInterval);
}

int HttpAccount::Verify::flushInterval() 
{
	return m_flushInterval;
}

int HttpAccount::Verify::validInterval() 
{
	return m_validInterval;
}

//QReadWriteLock* HttpAccount::Verify::lock()
//{
//	return &m_lock;
//}

QString HttpAccount::Verify::verify()
{
	QReadLocker lock(&m_lock);
	return m_verify;
}

QString HttpAccount::Verify::newVerify()
{
	QWriteLocker lock(&m_lock);
	m_verify =QString::number(random(100000, 999999));
	//刷新时间
	m_dateTime = QDateTime::currentDateTime();
	return m_verify;
}

void HttpAccount::Verify::setFlushInterval(int msec)
{
	m_flushInterval = msec;
}

void HttpAccount::Verify::setValidInterval(int msec)
{
	m_validInterval = msec;
}
